/*
 * @Descripttion: db管理器
 * @Version: 1.0
 * @Author: undead
 * @Date: 2021-03-27 11:18:44
 * @LastEditors: undead
 * @LastEditTime: 2021-04-09 16:34:30
 */
import IndexDB from "./index-db/indexDb";
import Queue from "./queue";

const IndexDBInstance: IndexDB = new IndexDB();

class DbManage {
    static dbManage: DbManage;
    static create() {
        if (!this.dbManage) {
            this.dbManage = new DbManage();
        }

        return this.dbManage;
    }
    async add(logInfo: object) {
        return await Queue.invokeQueue(async () => {
            const log = JSON.stringify(logInfo);
            await IndexDBInstance.addLog(log);
        });
    }
    async getDayInfo() {
        const result = await IndexDBInstance.getCurrentDayInfo();
        // console.log(result);
        return result;
    }
    async getDetails(logDay?: string, index?: number) {
        let logDataIndex: string;

        if (logDay && index) {
            logDataIndex = IndexDBInstance.logReportNameFormatter(logDay, index);
        } else {
            const getDayInfo = await this.getDayInfo();
            logDataIndex = IndexDBInstance.logReportNameFormatter(getDayInfo.logDay, getDayInfo.reportPagesInfo.pageSizes.length - 1);
        }

        const result = await IndexDBInstance.getLogsByReportName(logDataIndex);
        // console.log(result);
        return result;
    }
}
export default DbManage.create();
